package cn.sykj.car.service.impl;

import cn.sykj.car.common.Const;
import cn.sykj.car.entity.DictionaryDict;
import cn.sykj.car.exception.DictionaryDictException;
import cn.sykj.car.exception.ParameterInvalidException;
import cn.sykj.car.mapper.DictionaryDictMapper;
import cn.sykj.car.service.IDictionaryDictService;
import cn.sykj.car.service.IDictionaryService;
import cn.sykj.car.utils.Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 数据字典dict_group维护（手工维护） 服务实现类
 * </p>
 *
 * @author yixu
 * @since 2021-03-26
 */
@Service
public class DictionaryDictServiceImpl extends ServiceImpl<DictionaryDictMapper, DictionaryDict> implements IDictionaryDictService {

    @Resource
    IDictionaryService iDictionaryService;


    /**
     * 新增维护字段
     *
     * @param dictionaryDict
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Boolean addDictionaryDict(DictionaryDict dictionaryDict) throws Exception{
        //验证参数是否为空
        if(Utils.isEmpty(dictionaryDict)){
            throw  new ParameterInvalidException();
        }

        //验证
        if (Utils.isEmpty(dictionaryDict.getDictName())){
            throw new DictionaryDictException("","名不能为空");
        }
        //验证
        if (Utils.isEmpty(dictionaryDict.getDictValue())){
            throw new DictionaryDictException("","值不能为空");
        }
        // 验证值不能重复
        QueryWrapper<DictionaryDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DictionaryDict.DICT_VALUE,dictionaryDict.getDictValue());
        List<DictionaryDict> list = this.list(queryWrapper);
        if(Utils.notEmpty(list) && list.size() > 0){
            throw new DictionaryDictException("","值不能重复");
        }

        boolean save = this.save(dictionaryDict);

        return save;
    }

    /**
     * 修改维护字段
     *
     * @param dictionaryDict
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Boolean updateDictionaryDict(DictionaryDict dictionaryDict) throws Exception{
        //验证参数是否为空或者id为空
        if(Utils.isEmpty(dictionaryDict)||Utils.isEmpty(dictionaryDict.getId())){
            throw new ParameterInvalidException();
        }
        //判断原始记录是否存在
        DictionaryDict dict=this.getById(dictionaryDict.getId());
        if(Utils.isEmpty(dict)||dict.getDelFlag().equals(Const.DEL_FLAG_TRUE)){
            throw new DictionaryDictException("","记录不存在");
        }
        //验证
        if (Utils.isEmpty(dictionaryDict.getDictName())){
            throw new DictionaryDictException("","名不能为空");
        }
        // 验证
        if(Utils.isEmpty(dictionaryDict.getDictValue())){
            throw new DictionaryDictException("","值不能为空");
        }

        // 验证除了自己之外，值不能重复
        QueryWrapper<DictionaryDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DictionaryDict.DICT_VALUE,dictionaryDict.getDictValue());
        queryWrapper.ne(DictionaryDict.ID,dictionaryDict.getId());
        List<DictionaryDict> list = this.list(queryWrapper);
        if(Utils.notEmpty(list) && list.size() > 0){
            throw new DictionaryDictException("","值不能重复");
        }

        // 把要更新的对象中的属性，覆盖到已有对象中
        boolean updateById = baseMapper.updateDictionaryDict(dictionaryDict) > 0;
        return updateById;
    }

    /**
     * 删除字典表维护字段
     *
     * @param
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Boolean deleteDictionaryDict(Integer id) {

        //验证参数是否为空
        if (Utils.isEmpty(id)){
            throw new ParameterInvalidException();
        }
        DictionaryDict dictionaryDict=this.getById(id);
        if (Utils.isEmpty(dictionaryDict)||dictionaryDict.getDelFlag().equals(Const.DEL_FLAG_TRUE)){
            throw new DictionaryDictException("","记录不存在");
        }

        boolean a=this.removeById(id);

        return a;
    }

}
